<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface
namespace app\admin\controller;

use app\common;
use think\Controller;
use think\Session;
use think\Request;
use think\File;
class Member extends common
{
    //列表
    public function index()
    {
        $where = '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        $id = (isset($_GET['id'])) ? trim($_GET['id']) : '';
        //搜索条件

        if (strlen($id)) {
            $where = "id='{$id}'";
        }
        //搜索条件
        if ($keyword){
            $where = "title LIKE '%{$keyword}%' OR writer LIKE '%{$keyword}%' OR content LIKE '%{$keyword}%'";
        }

        //查询总数据
        $maxpages = Db('member')->where($where)->count('*');
        //最大页数
        $maxpage = $maxpages / 10;
        // 查询状态为0的数据 并且每页显示10条数据 总记录数为1000
        $list = db('member')->where($where)->paginate(20, $maxpages);
//        return $this->sucReturn($list);exit;
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $this->assign("id",$id);
        $this->assign("keyword",$keyword);
        $this->assign('list', $list);
        $this->assign('loginname', $this->login());
        $this->assign('page', $page);
        $this->assign('maxpage', $maxpage);
        $this->assign('maxpages', $maxpages);
        $this->assign('empty', '<span class="empty">没有数据</span>');

        // 渲染模板输出
        return $this->fetch('member/index');
    }

   //增减积分页面
    public function jifen(){
        $id = $_GET['id'];
        //查出用户数据
        $member = db("member")
            ->where("id","=","{$id}")
            ->field("id,name")
            ->find();
//        var_dump($member);exit;
        $this->assign('info', $member);
        $this->assign('loginname', $this->login());
        return $this->fetch("member/integral");

    }

//积分增减操作
    public function do_jifen()
    {
        $jifen=$_POST['jifen'];
        if($jifen==''){
            $this->error("积分不能为空");
        }
        $data=array();
        $data['user_id'] = $_POST['user_id'];
        $data['username'] = $_POST['username'];
        $data['bank'] = $_POST['bank'];
        $data['jifen'] = $jifen;
        $data['reason'] = $_POST['reason'];
        $data['note'] = $_POST['note'];
        $data['type'] = $_POST["type"];
        $data['add_time'] = time();
        //查看该用户积分
        $member = db("member")
            ->where("id","=","{$_POST['user_id']}")
            ->where("name","=","{$_POST['username']}")
            ->field("id,name,all_integral,use_integral")
            ->find();
        //增加用户的总积分和可用积分
        $new_data = array();
        $new_data['all_integral'] = $member['all_integral'] + $jifen;;
        $new_data['use_integral'] = $member['use_integral'] + $jifen;

        if($member){
            //将新的积分添加到数据库中（用户表）
            $new = db("member")
                ->where("id","=","{$_POST['user_id']}")
                ->update($new_data);
//            var_dump($new);exit;
        }else{
            $this->error("没有找到该用户信息");
        }

        $new_member = db("member")
            ->where("id","=","{$_POST['user_id']}")
            ->where("name","=","{$_POST['username']}")
            ->field("id,name,all_integral,use_integral")
            ->find();
//        var_dump($new_member);exit;
        $data['balance'] = $new_member["use_integral"];
//        var_dump($data);exit;
        $record = db("record")
            ->insertGetId($data);

        if($record >0){
            $this->success("积分增加成功", "member/index");
        }else{
            $this->error("给该用户积分增加失败");
        }

    }





    //删除数据
    public function delete($id,$username)
    {
        $list = db("member")->find($id);
        if ($list > 0) {
            $sql = db('member')->where("id","=", $id)->delete();
            if ($sql) {
                $sql1= db('cash')->where("user_id","=", $id)->where("username","=","{$username}")->delete();
                $sql2= db('record')->where("user_id","=",$id)->where("username","=","{$username}")->delete();
                if($sql1 && $sql2){
                    $this->success("删除成功", "member/index");
                }else{
                    $this->error("申请记录删除失败");
                }
            } else {
                $this->error("删除失败");
            }
        }
    }

}